package com.swind.fast.modules.app.halali.partner.controller;

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.swind.fast.common.api.vo.Result;
import com.swind.fast.common.system.query.QueryGenerator;
import com.swind.fast.common.util.oConvertUtils;
import com.swind.fast.common.constant.CommonConstant;
import com.swind.fast.modules.app.halali.partner.entity.RestaurantMeal;
import com.swind.fast.modules.app.halali.partner.service.IRestaurantMealService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import com.swind.fast.common.system.base.controller.SwindController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
// import com.swind.fast.common.aspect.annotation.AutoLog;

import com.swind.fast.modules.system.entity.SysAnnouncement;
import com.swind.fast.modules.system.entity.SysAnnouncementSend;
import com.swind.fast.modules.system.service.ISysAnnouncementSendService;
import com.swind.fast.modules.system.service.ISysAnnouncementService;

import com.swind.fast.modules.app.halali.partner.vo.response.MealInfoVo;
import com.swind.fast.modules.app.halali.partner.vo.request.MealVo;
import com.swind.fast.modules.app.halali.partner.vo.request.RankInfoVo;
import com.swind.fast.modules.app.halali.partner.component.HalaliUtils;
import org.springframework.http.MediaType;
import com.swind.fast.modules.app.halali.partner.vo.common.FileUploadVo;
import com.swind.fast.modules.app.halali.partner.entity.RestaurantCategory;
import com.swind.fast.modules.app.halali.partner.entity.RestaurantMealCategory;
import com.swind.fast.modules.app.halali.partner.service.IRestaurantMealCategoryService;

 /**
 * @Description: 餐厅
 * @Author: swind
 * @Date:   2022-01-20
 * @Version: V1.0
 */
@Api(tags="05.商户端-菜品")
@RestController
@RequestMapping("/app/partner/meal")
@Slf4j
public class RestaurantMealController extends SwindController<RestaurantMeal, IRestaurantMealService> {
	@Autowired
	private HalaliUtils halaliUtils;

	@Autowired
	private IRestaurantMealCategoryService mealCategoryService;

	@ApiOperation(value="菜品-列表v2", notes="")
	@GetMapping(value = "/list/v2")
	public Result<List<RestaurantCategory>> queryMealListV2Action() {
		// List<RestaurantCategory> list = this.service.getMealListv2();
		List<RestaurantCategory> list = this.service.getMealListv3();
		return Result.OK(list);
	}

	@ApiOperation(value="菜品-列表", notes="")
	@GetMapping(value = "/list")
	public Result<List<RestaurantCategory>> queryMealListAction() {
		List<RestaurantCategory> list = this.service.getMealListv1();
		return Result.OK(list);
	}

	@ApiOperation(value="菜品-新增", notes="")
	@PostMapping(value = "/add")
	public Result<MealInfoVo> addAction(@RequestBody() MealVo mealVo) {
		MealInfoVo result = this.service.saveMain(mealVo);
		return Result.OK(result);
	}

	@ApiOperation(value="菜品-更新", notes="菜品-更新")
	@PostMapping(value = "/update")
	public Result<MealInfoVo> updateAction(@RequestBody() MealVo mealVo) {
		MealInfoVo result = this.service.updateInfo(mealVo);
		return Result.OK(result);
	}

	@ApiOperation(value="菜品-排序", notes="")
	@PostMapping(value = "/rank/update")
	public Result<?> rankUpdateAction(@RequestBody List<RankInfoVo> items) {
		this.service.updateRank(items);
		return Result.OK();
	}

	@ApiOperation(value="菜品-详情", notes="")
	@GetMapping(value = "/detail")
	public Result<RestaurantMeal> detailAction(@RequestParam(name="id", required=true) String id) {
		RestaurantMeal item = this.service.getDetailById(id);
		this.service.renderPic(item);
		if (item == null) {
			return Result.errorT(null);
		}
		return Result.OK(item);
	}

	@ApiOperation(value="菜品-图片", produces=MediaType.MULTIPART_FORM_DATA_VALUE)
    @PostMapping(value = "/update/pic", headers="content-type=multipart/form-data")
    public Result<FileUploadVo> updatePic(HttpServletRequest request,HttpServletResponse response) {
    	MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile("file");// 获取上传文件对象
        String bizPath = "halali/meal";
        FileUploadVo fileVo = halaliUtils.uploadLocal(file, bizPath);
        return Result.OK(fileVo);
    }

    @ApiOperation(value="菜品-删除", notes="")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		this.service.removeById(id);
		return Result.OK();
	}

	@ApiOperation(value="删除分类", notes="")
	@DeleteMapping(value = "/category/delete")
	public Result<?> deleteCategory(@RequestParam(name="id",required=true) String id) {
		this.mealCategoryService.removeById(id);
		return Result.OK();
	}
}
